package com.ponhu.ea2.goodsCenter.provider.facade.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.constants.CommonConstant;
import com.ponhu.ea2.dto.LogsForTextAddDto;
import com.ponhu.ea2.dto.UpdateImagesDto;
import com.ponhu.ea2.entity.Inventory;
import com.ponhu.ea2.enums.SyncAppletTypeEnum;
import com.ponhu.ea2.enums.YesOrNoEnum;
import com.ponhu.ea2.goodsCenter.provider.biz.OperationRecordLogsCoreService;
import com.ponhu.ea2.goodsCenter.provider.service.InventoryService;
import com.ponhu.ea2.goodsCenter.provider.service.PhinventoryToAmqpService;
import com.ponhu.ea2.service.facade.GoodsCenterForWMSService;
import com.ponhu.ea2.service.facade.UpdateInventoryImageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.aop.framework.AopContext;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

/**
 * @author luyu
 * @create 2021-10-19 16:36
 */
@Slf4j
@DubboService(version = "1.0.0", timeout = 6000)
public class UpdateInventoryImageServiceImpl implements UpdateInventoryImageService {

    @Resource
    InventoryService inventoryService;

    @Resource
    private OperationRecordLogsCoreService operationRecordLogsCoreService;

    @Resource
    private GoodsCenterForWMSService goodsCenterForWMSService;

    @Resource
    PhinventoryToAmqpService phinventoryToAmqpService;

    @Override
    public ResponseBean updateImageBySku(UpdateImagesDto updateImagesDto) {
        log.info("更新白底图开始,货品编码为:{}",updateImagesDto.getGoodsCode());
        // 更新货品图片
        Inventory inventory = inventoryService.getOne(
                new LambdaQueryWrapper<Inventory>().eq(Inventory::getInventoryCode,
                        updateImagesDto.getGoodsCode()));
        if(ObjectUtils.isEmpty(inventory)){
            log.error("货品编码为{}的货品不存在",updateImagesDto.getGoodsCode());
            return ResponseBean.error("货品编码为"+updateImagesDto.getGoodsCode()+"的货品不存在");
        }
        if(!((UpdateInventoryImageServiceImpl) AopContext.currentProxy())
                .updateInventoryImg(updateImagesDto, inventory)){
            return ResponseBean.error("更新失败");
        }
        // 记录日志
        CompletableFuture.runAsync(() -> {
            operationRecordLogsCoreService.addTextAddOperationRecordLogs(
                    new LogsForTextAddDto()
                            .setBusinessTable(CommonConstant.BUSINESS_TYPE_INVENTORY)
                            .setRecordId(inventory.getId())
                            .setOperator("外部摄影系统")
                            .setOperatorId(0L)
                            .setTextContent("更新货品白底图"));
        });
        // 同步wms
        CompletableFuture.runAsync(() -> {
            goodsCenterForWMSService.createOrUpdateGoodsForWms(inventory);
        });
        // 同步会员小程序
        CompletableFuture.runAsync(() ->{
            phinventoryToAmqpService.pushDataToAmqp(
                    Arrays.asList(inventory.getInventoryCode()),
                    SyncAppletTypeEnum.PH_INVENTORY_DATA.getCode());
        });
        log.info("更新白底图结束,入参为:{},结果为:{}",updateImagesDto,"更新成功");
        return ResponseBean.ok("更新成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean updateInventoryImg(UpdateImagesDto updateImagesDto,Inventory inventory){
        // 更新货品图片
        return inventoryService.updateById(
                inventory.setDetailImage(updateImagesDto.getDetailImage())
                        .setMainImage(updateImagesDto.getDetailImage().split("\\|")[0])
                        .setBaseMap(YesOrNoEnum.YES.getCode()));
    }
}
